﻿using Alison.EFCore;
using Alison.Tools.HttpContact.Response;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.EntityFrameworkCore;
using System.Threading.Tasks;
using Alison.EFCore.Dto;

namespace Alison.Service
{
	public class AuthItemService : IAuthItemService
	{
		private readonly ILogger<AuthItemService> _logger;
		public AuthItemService(ILogger<AuthItemService> logger) {
			_logger = logger;
		}

		public async Task<ApiResponse> GetAll() {
			try {
				using var db = new AlisonDbContext();
				var model = await db.Authitems.ToListAsync();

				return new ApiResponse(200, model.OrderBy(t => t.Id));
			} catch (Exception ex) {
				_logger.LogError(ex, "");
				return new ApiResponse(500, ex.Message);
			}
		}
		public async Task<ApiResponse> GetAll(AuthItemDto dto) {
			try {
				using var db = new AlisonDbContext();
				var model = await db.Authitems.Where(t =>
				t.Authname.Contains(dto.AuthName) ||
				string.IsNullOrWhiteSpace(dto.AuthName)).ToListAsync();

				return new ApiResponse(200, model.OrderBy(t => t.Id));
			} catch (Exception ex) {
				_logger.LogError(ex, "");
				return new ApiResponse(500, ex.Message);
			}
		}
	}
}
